package com.alan.controller;

import com.alan.entity.admin;
import com.alan.entity.teach;
import com.alan.entity.teacherstudent;
import com.alan.repository.AdminRepository;
import com.alan.repository.StudentRepository;
import com.alan.repository.impl.AdminRepsitoryImpl;
import com.alan.repository.impl.StudentRepositoryImpl;
import com.alan.service.AdminService;
import com.alan.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();
    private AdminRepository adminRepository = new AdminRepsitoryImpl();
    private StudentRepository studentRepository = new StudentRepositoryImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        HttpSession session = (HttpSession) req.getSession();
        switch (method) {
            case "studentinfo":
                String pagestr = req.getParameter("page");
                Integer page = Integer.parseInt(pagestr);
                List<teacherstudent> list = adminService.findall(page);
                req.setAttribute("list", list);
                req.setAttribute("dateprepage", 10);
                req.setAttribute("currentpage", page);
                req.setAttribute("sumpages", adminService.getSumpages());
                req.getRequestDispatcher("/admin/StudentInfo.jsp").forward(req, resp);
                break;
            case "marks":
                pagestr = req.getParameter("page");
                Integer page1 = Integer.parseInt(pagestr);
                List<teacherstudent> list1 = adminService.findall(page1);
                req.setAttribute("list1", list1);
                req.setAttribute("dateprepage", 10);
                req.setAttribute("currentpage", page1);
                req.setAttribute("sumpages", adminService.getSumpages());
                req.getRequestDispatcher("/admin/StudentMarks.jsp").forward(req, resp);
                break;
            case "teacher":
                pagestr = req.getParameter("page");
                Integer page2 = Integer.parseInt(pagestr);
                List<teach> list2 = adminService.findallteachers(page2);
                req.setAttribute("list2", list2);
                req.setAttribute("dateprepage",10);
                req.setAttribute("currentpage",page2);
                req.setAttribute("sumpages", adminService.getteacherpages());
                req.getRequestDispatcher("/admin/TeacherInfo.jsp").forward(req, resp);
                break;
            case "adminself":
                req.setAttribute("admin", session.getAttribute("admin"));
                req.getRequestDispatcher("/admin/adminself.jsp").forward(req, resp);
                break;
            case "update":
                req.setAttribute("admin", session.getAttribute("aadmin"));
                req.getRequestDispatcher("/admin/adminselfchange.jsp").forward(req, resp);
                break;
            case "out":
                session.invalidate();
                req.setAttribute("info","退出成功！您辛苦了！");
                req.getRequestDispatcher("/Confirm_or_cancel/outsuccess.jsp").forward(req,resp);
                break;


        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = (HttpSession) req.getSession();
        String method = req.getParameter("method");
        switch (method) {
            case "update":
                String name = req.getParameter("name");
                String password = req.getParameter("password");
                admin admin = (admin) session.getAttribute("admin");
                adminRepository.change(name, password, admin.getName(), admin.getPassword());
                resp.sendRedirect("/admin?method=adminself");
                break;

        }

    }
}
